package com.huxiu;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.google.gson.Gson;
import com.huxiu.net.GsonRequest;
import com.huxiu.net.NetworkConstants;
import com.huxiu.net.responses.ProfileResponse;
import com.huxiu.utils.Constants;
import com.huxiu.utils.NetUtil;
import com.huxiu.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

import java.util.LinkedHashMap;
import java.util.Map;

public class SanFangBindHxActivity extends BaseActivity implements View.OnClickListener {
    //登录界面
    @ViewInject(R.id.username_edit)
    private EditText usernameEdit;
    @ViewInject(R.id.password_edit)
    private EditText passwordEdit;
    @ViewInject(R.id.log_in_button)
    private Button dengluButton;
    @ViewInject(R.id.back)
    private ImageView back;
    private int typeId;
    private String token;
    private String openId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_san_fang_bind_hx);
        ViewUtils.inject(this);
        dengluButton.setOnClickListener(this);
        back.setOnClickListener(this);
        typeId = getIntent().getIntExtra("typeIDguanlian", 0);
        token = getIntent().getStringExtra("tokenStr");
        openId = getIntent().getStringExtra("openIdStr");
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.log_in_button:
                if (inputValid()) {
                    if (!NetUtil.checkNet(this)) {
                        Utils.showToast(R.string.generic_check);
                        return;
                    }
                    zuceBind();
                }
                break;
            case R.id.back:
                finish();
                break;
        }

    }

    //注册成功后绑定刚刚注册的虎嗅账号
    private void zuceBind() {
        String url = NetworkConstants.USER_BINDING_URL;
        Map<String, String> params = new LinkedHashMap<>();
        switch (typeId) {
            case 1:
                params.put("oauth_type", "qq");
                break;
            case 2:
                params.put("oauth_type", "sina");
                break;
            case 3:
                params.put("oauth_type", "weixin");
                break;
        }
        params.put("oauth_uid", openId);
        params.put("oauth_token", token);
        params.put("username", usernameEdit.getText().toString());
        params.put("password", passwordEdit.getText().toString());
        GsonRequest<ProfileResponse> request = new GsonRequest<>(url, Request.Method.POST,
                ProfileResponse.class, false, params, new Response.Listener<ProfileResponse>() {
            @Override
            public void onResponse(ProfileResponse response) {
                if (response.isSuccess()) {
                    Utils.showToast("绑定成功");
                    Intent intent = new Intent(Constants.INTENT_USER_LOGGED_IN);
                    Gson gson = new Gson();
                    String json = gson.toJson(response.data);
                    intent.putExtra("loginJson", json);
                    LocalBroadcastManager.getInstance(APP.getInstance()).sendBroadcast(intent);
                    setResult(RESULT_OK);
                    finish();
                } else {
                    Utils.showToast(response.error.message);
                }
            }
        }, mErrorListener);
        mQueue.add(request);
    }

    //下面是登录相关接口
    private boolean inputValid() {
        if (TextUtils.isEmpty(usernameEdit.getText().toString())) {
            Utils.showToast(R.string.invalid_username);
            return false;
        }
        if (TextUtils.isEmpty(passwordEdit.getText().toString())) {
            Utils.showToast(R.string.invalid_password);
            return false;
        }
        return true;
    }



    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }
}
